//------------------------------------------------------------------------------
//!	@file	CreateLUT.c
//!	@brief
//!		LUT creation tool
//!
//!	@Description
//!		Create a binary LUT object from LUT text files.
//!		This program requires three ASCII files:
//!		red.txt (red), green.txt (green), blue.txt (blue)
//!		These files are read, processed and dumped as lut tables.
//!
//!	Originally Developed: July, 1997 to October, 1999
//!
//!	$Id: CreateLut.c 158 2013-03-03 07:00:15Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//
//	This file is part of CreateLUT: A LUT creation library.
//
//	CreateLUT is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CreateLUT is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with CreateLUT.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "ScanArgs.h"
#include "ProcRGB.h"		//	Process RGB files
#include "ProcGray.h"		//	Process Gray only files (8bit)
#include "Proc10bit.h"		//	Process Gray only files (10bit)
#include "ProcCorner.h"		//	Process Gray Corner cases gain min/max gamma min/max
#include "ProcEOCtrl.h"		//	Process EO files
#include "ProcGamma.h"		//	Create gamma tables directly
#include "ProcOTF.h"		//	On the fly table generation

#include "Columnize.h"		//	Generate debug file
#include "bi.h"				//	Build information

void Usage(void);
void Version(void);

//
//	Configuration parameters
//
double		StartGamma	= 0.0;
double		EndGamma	= 0.0;
int32_t		LutGammaCt	= 0;

double		StartGain	= 0.0;
double		EndGain		= 0.0;
int32_t		LutGainCt	= 0;

uint32_t	ProcessCtrl	= 0;
uint32_t	Debug		= 0;
bool		NoDSP		= false;

char		InFileName[_MAX_PATH];
char		OutFileName[_MAX_PATH];
char		DebugFileName[_MAX_PATH];


//
//	Name:
//		main
//
//	Description:
//		Main drives the program.
//		Main does argument testing and calls the process files.
//
//	Parameters:
//		Int16	argc:	Argument count
//		String	argv[]:	Argument string array
//
//	Returns:
//		(None)
//
int main(int argc, char* argv[])
{

//
//	Initialize variables
//
	strcpy(DebugFileName, "debug.txt");

//
//	Scan arguments
//
	switch (argc) {
	default:
		Usage();
		exit(1);
		break;

	case 1:
		Version();
		exit(1);
		break;

	case 2:
		ScanArgs(argv[1]);
		break;

	case 3:
		ScanArgs(argv[1]);
		strcpy(InFileName, argv[2]);
		break;

	case 4:
		ScanArgs(argv[1]);
		strcpy(InFileName, argv[2]);
		strcpy(OutFileName, argv[3]);
		break;

	case 5:
		ScanArgs(argv[1]);
		strcpy(InFileName, argv[2]);
		strcpy(OutFileName, argv[3]);
		strcpy(DebugFileName, argv[4]);
		break;
	}

//
//	Process the world
//
	switch (ProcessCtrl & 0x0f) {
	case 0x00:
		RGB_Execute();
		break;
	case 0x01:
		EOCtrl_Execute();
		break;
	case 0x02:
		Gamma_Execute();
		break;
	case 0x03:
		OTF_Execute();
		break;
	case 0x04:
		Gray_Execute();
		break;
	case 0x05:
		Proc10bit_Execute(NO_EO);
		break;
	case 0x06:
		Proc10bit_Execute(READ_EO);
		break;
	case 0x07:
		ProcCorner_Execute();
		break;

	default:
		printf("Unknown process option: %x\n", ProcessCtrl);
		break;
	}

//
//	Dump debug file if necessary
//
	if (Debug & 0x01) Columnize();

//
//	Exit!
//
	return(0);
}


//
//	Name:
//		Usage
//
//	Description:
//		Prints the usage information about the program.
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
void Usage(void)
{
	printf("Usage:\n");
	printf("\tCreateLut <cfg file>\n");
	printf("\tCreateLut <cfg file> <input file>\n");
	printf("\tCreateLut <cfg file> <input file> <output file>\n");
	printf("\tCreateLut <cfg file> <input file> <output file> <debug file>\n");
}


//
//	Name:
//		Usage
//
//	Description:
//		Prints the version information about the program.
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
void Version(void)
{
	printf("Version Information:\n");
	bldinfoPrintBldInfo();
}

//
//	 End: CreateLut.c
//

